import { Prefab, resources } from "cc";
import { ppActiveMaster } from "./popup.active.master";
import { ppCongratulation } from "./popup.congratulation";
import { ppDefeat } from "./popup.defeat";
import { ppDiamond } from "./popup.diamond";
import { ppDiamondFruit } from "./popup.diamond.fruit";
import { ppDrawerLock } from "./popup.drawerlock";
import { ppGiftBlocker } from "./popup.gift_blocker";
import { ppProp } from "./popup.prop";
import { ppResurgence } from "./popup.resurgence";
import { ppTreasureBox } from "./popup.treasurebox";
import { ppVictory } from "./popup.victory";
import { ppWait } from "./popup.wait";

export enum EPopup {
	DEFEAT,
	VICTORY,
	DIAMOND,
	DRAWER_LOCK,
	GIFT_BLOCKER,
	PROP,
	RESURGENCE,
	TREASUREBOX,
	WAIT,
	CONGRATULATION,
	DIAMOND_FRUIT,
	ACTIVE_MASTER,
}

const CONFIG = {
	[EPopup.DEFEAT]: {
		component: ppDefeat,
		path: 'defeat',
		script: 'popup.defeat',
		prefab: null
	},
	[EPopup.VICTORY]: {
		component: ppVictory,
		path: 'victory',
		script: 'popup.victory',
		prefab: null
	},
	[EPopup.DIAMOND]: {
		component: ppDiamond,
		path: 'diamond',
		script: 'popup.diamond',
		prefab: null
	},
	[EPopup.DRAWER_LOCK]: {
		component: ppDrawerLock,
		path: 'drawer-lock',
		script: 'popup.drawerlock',
		prefab: null
	},
	[EPopup.GIFT_BLOCKER]: {
		component: ppGiftBlocker,
		path: 'gift-blocker',
		script: 'popup.gift_blocker',
		prefab: null
	},
	[EPopup.PROP]: {
		component: ppProp,
		path: 'prop',
		script: 'popup.prop',
		prefab: null
	},
	[EPopup.RESURGENCE]: {
		component: ppResurgence,
		path: 'resurgence',
		script: 'popup.resurgence',
		prefab: null
	},
	[EPopup.TREASUREBOX]: {
		component: ppTreasureBox,
		path: 'treasurebox',
		script: 'popup.treasurebox',
		prefab: null
	},
	[EPopup.WAIT]: {
		component: ppWait,
		path: 'wait',
		script: 'popup.wait',
		prefab: null
	},
	[EPopup.CONGRATULATION]: {
		component: ppCongratulation,
		path: 'congratulation',
		script: 'popup.congratulation',
		prefab: null
	},
	[EPopup.DIAMOND_FRUIT]: {
		component: ppDiamondFruit,
		path: 'diamond-fruit',
		script: 'popup.diamond.fruit',
		prefab: null
	},
	[EPopup.ACTIVE_MASTER]: {
		component: ppActiveMaster,
		path: 'active-master',
		script: 'popup.active.master',
		prefab: null
	}
}

Reflect.ownKeys(CONFIG).forEach(key=>{
	let conf = CONFIG[key];
	conf.path = 'prefab/pop-up/' + conf.path;
	conf.script = './file_name.ts'.replace('file_name', conf.script);
});

export async function GetPopPrefab(p: EPopup): Promise<Prefab> {

	let conf = CONFIG[p];

	if(!conf.prefab){
		conf.prefab = await mtec.cc.loadResAsync(conf.path, Prefab, resources);
	}

	return conf.prefab;
}

export async function GetPopComponent<P extends EPopup>(p: P): Promise<(typeof CONFIG)[P]['component']> {
	let conf = CONFIG[p];
	if(!conf.component){
		conf.component = await import(conf.script).then(m=>m.default);
	}

	return conf.component;
}

export type PopCtor<P extends EPopup> = typeof CONFIG[P]['component'];

export type PopIns<P extends EPopup> = InstanceType<PopCtor<P>>;

export type PopParams<P extends EPopup> = Parameters<PopIns<P>['onShow']>;

export type PopReturn<P extends EPopup> = Awaited<ReturnType<PopIns<P>['show']>>;